<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('收费标准')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <style type="text/css">
        table label.error {
            position: inherit;
        }

        select + label.error {
            z-index: 1;
            right: 40px;
        }
    </style>
</head>
<body class="gray-bg">
<div class="main-content">

    <div class="row">
        <div class="col-sm-4">
            <h2>[[${course.name}]]</h2>
        </div>
    </div>
    <form id="form-add" class="form-horizontal">
        <div class="row" style="padding-bottom: 5px">
            <div class="col-sm-3">
                <select id="schoolId" name="schoolId" class="form-control"
                        th:with="types=${schools}"
                        th:onchange="changeSchool(this)">
                    <option th:text="开课校区" th:value="0"></option>
                    <option th:each="type : ${types}" th:text="${type.name}"
                            th:value="${type.schoolId}"></option>
                </select>
            </div>
        </div>
    </form>
    <div class="row" style="padding: 10px 0">
        <div class="col-sm-6">
            <button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i>
            </button>
            <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i>
            </button>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i
                    class="fa fa-check"></i>保 存
            </button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i
                    class="fa fa-reply-all"></i>关 闭
            </button>
        </div>
    </div>

</div>


<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<script th:src="@{/js/jquery.tmpl.js}"></script>
<script th:inline="javascript">
    var $courseId = [[${course.courseId}]];

    function changeSchool(obj) {
        $.table.search();
    };

    $(function () {
        $("#schoolId option").not("[value=0]").prop("selected", true);
        var schoolId = $("#schoolId");
        var options = {
            url: ctx + "teaching/price/list",
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [{
                checkbox: true
            },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                        var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                        return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'lessonCount',
                    align: 'center',
                    title: '数量（课时）',
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='lessonCount' value='%s' style='text-align:center'>", value);
                        return html;
                    }
                },
                {
                    field: 'lessonPrice',
                    align: 'center',
                    title: '总价',
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='lessonPrice' value='%s' style='text-align:center'>", value);
                        return html;
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
        };
        $.table.init(options);
    });

    $.validator.addMethod("goodsName", function (value, element) {
        return !this.optional(element);
    }, "商品名称必填。");

    $.validator.addMethod("goodsWeight", function (value, element) {
        return !this.optional(element) && (value <= 100 && value >= 0);
    }, "商品重量长度区间0-100。");

    /* 主子表-提交 */
    function submitHandler(index, layero) {
        var schoolId = $("#schoolId").val();
        if (schoolId == null || schoolId == undefined || schoolId == 0) {
            $.modal.alert("请选择开课校区");
            return false;
        }
        if ($.validate.form()) {

            var dataAfter = [];
            var trs = $("#bootstrap-table").find("tbody tr");
            $.each(trs, function (index, value) {
                var obj = {};
                obj.lessonCount = $(value).find("input[name='lessonCount']").val();
                obj.lessonPrice = $(value).find("input[name='lessonPrice']").val();
                dataAfter.push(obj);
            })
            console.log(dataAfter);
            var postUrl = $.common.sprintf(ctx + "teaching/price/addList?courseId=%s&schoolId=%s", $courseId, schoolId);
            $.ajax({
                url: postUrl,
                method: "post",
                contentType: "application/json",
                data: JSON.stringify(dataAfter),
                success: function (response) {
                    // console.log(response);
                    $.modal.alertSuccess(response.msg);
                    $.table.search();
                }
            })
            // $.operate.saveModal(postUrl, );
        }
    }

    $("input[name='birthday']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    function addRow() {
        var count = $("#" + table.options.id).bootstrapTable('getData').length;
        var row = {
            index: $.table.serialNumber(count),
            lessonCount: "",
            lessonPrice: ""
        }
        sub.addRow(row);
    }

    $("#bootstrap-table").on("post-body.bs.table", function (e, args) {
        $("input[name$='date']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true,
            pickerPosition: 'top-right'
        });
    });
</script>
</body>
</html>

<!-- 商品类型 -->
<script id="goodsTypeTpl" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='goods[${index}].type'>
    <option value="">所有</option>
    <option value="0" {{if type==="0"}}selected{{/if}}>寒性</option>
    <option value="1" {{if type==="1"}}selected{{/if}}>热性</option>
</select>
</div>
















































































</script>
